package base;

import java.sql.*;
import javax.naming.Context;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;

public abstract class BaseDAO {
    public BaseDAO() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    boolean isTestCase = true;
    public Connection conn;
    /**
     *	Obtain the connection
     */
    public Connection getConnection() throws Exception {

        String ip=null;
        String port=null;
        String dbname=null;
        String dbpwd=null;
        String  username=null;
        try {
            Properties pro=new Properties();
        try {
            pro.load(new FileInputStream("d:/config.txt"));
            //System.out.println(pro.getProperty("serverIP"));
            ip=pro.getProperty("serverIP");
            port=pro.getProperty("port");
            dbname=pro.getProperty("dbname");
            username=pro.getProperty("username");
            dbpwd=pro.getProperty("dbpwd");

        } catch (IOException ex) {
            ex.printStackTrace();
        }

            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://"+ip+":"+port+";SelectMethod=cursor;databaseName="+dbname,username,dbpwd);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

    /**
     *	Closes the connection
     */
    public Connection closeConnection(Connection con) {
        try {
            if (con != null) {
                con.close();
            } else {
                System.out.println("Connection is not open ");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    /**
     * Closes the resultset
     */
    public void closeResultSet(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    /**
     * Closes the prepared statement
     */
    public void closePreparedStatement(PreparedStatement pstmt) {
        try {
            if (pstmt != null) {
                pstmt.close();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    /**
     * Closes the statement
     */
    public void closeStatement(Statement stmt) throws Exception {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void closeCursors(Connection con, PreparedStatement pstmt) throws
            Exception {
        closeStatement(pstmt);
        closeConnection(con);
    }

    public void closeCursors(Connection con, Statement stmt, ResultSet rs) throws
            Exception {
        closeResultSet(rs);
        closeStatement(stmt);
        closeConnection(con);
    }

    private void jbInit() throws Exception {
    }

}
